package bsc.war.populate;

import bsc.ejb.entity.Comparator;
import bsc.ejb.entity.ComparatorDimensionalCut;
import bsc.ejb.entity.Dimension;
import bsc.ejb.entity.DimensionMember;
import bsc.ejb.entity.DimensionalCut;
import bsc.ejb.entity.DimensionalCutMember;
import bsc.ejb.entity.Map;
import bsc.ejb.entity.Objective;
import bsc.ejb.entity.Perspective;
import bsc.ejb.entity.Pointer;
import bsc.ejb.entity.ScoreCardDimension;
import bsc.ejb.entity.ScoreCardRelation;
import bsc.ejb.metadata.Comparation;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author luisZavaleta
 */
public class NewPointerDimMem extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

        //Create Map Scorecard
        Map mapScorecard = (Map) Factory.getBean("Map");
        mapScorecard.setName("Mapa de Finanzas");
        delegate.createMap(mapScorecard);

        //Create Perspective Scorecard
        Perspective perspectiveScorecard = (Perspective) Factory.getBean("Perspective");
        perspectiveScorecard.setName("Perspectiva de Finanzas");
        delegate.createPerspective(perspectiveScorecard);

        //Create Objetive Scorecard
        Objective objectiveScorecard = (Objective) Factory.getBean("Objective");
        objectiveScorecard.setName("F1. Mejorar las utilidades de la empresa");
        delegate.createObjective(objectiveScorecard);


        





        //Create Perspective Scorecard

        //Populate a new KPI
        Pointer pointer = (Pointer) Factory.getBean("Pointer");
        pointer.setName("F1.1 Ventas Netas");
        pointer.setDescription("Indicador de prueba");
        delegate.createPointer(pointer);

        //Populate a new Dimension
        Dimension dimension = (Dimension) Factory.getBean("Dimension");
        dimension.setName("Region");
        delegate.createDimension(dimension);

        //Populate a new Member
        DimensionMember dimensionmember1 = (DimensionMember) Factory.getBean("DimensionMember");
        dimensionmember1.setName("TOTAL_REGION");
        dimensionmember1.setDimension(dimension);
        delegate.createDimensionMember(dimensionmember1);

        DimensionMember dimensionmember2 = (DimensionMember) Factory.getBean("DimensionMember");
        dimensionmember2.setName("MX");
        dimensionmember2.setDimension(dimension);
        dimensionmember2.setDimensionMember(dimensionmember1);
        delegate.createDimensionMember(dimensionmember2);

        DimensionMember dimensionmember3 = (DimensionMember) Factory.getBean("DimensionMember");
        dimensionmember3.setName("CR");
        dimensionmember3.setDimension(dimension);
        dimensionmember3.setDimensionMember(dimensionmember1);
        delegate.createDimensionMember(dimensionmember3);


        //Populate a new IndDim
        ScoreCardDimension scorecarddimension = (ScoreCardDimension) Factory.getBean("ScoreCardDimension");
        scorecarddimension.setScoreCard(pointer);
        scorecarddimension.setDimension(dimension);
        delegate.createScoreCardDimension(scorecarddimension);

        //CreateComparation
        Comparation comparationReal = (Comparation) Factory.getBean("Comparation");
        comparationReal.setName("Real");
        comparationReal.setType(Comparation.Type.REAL);

        Comparation comparationGoal = (Comparation) Factory.getBean("Comparation");
        comparationGoal.setName("Meta");
        comparationGoal.setType(Comparation.Type.GOAL);

        delegate.createComparation(comparationReal);
        delegate.createComparation(comparationGoal);


        //Populate a new Comparador Real
        Comparator comparatorR = (Comparator) Factory.getBean("Comparator");
        comparatorR.setDescription("Real");
        comparatorR.setComparation(comparationReal);
        comparatorR.setPointer(pointer);
        delegate.createComparator(comparatorR);

        //Populate a new Comparador Meta
        Comparator comparatorM = (Comparator) Factory.getBean("Comparator");
        comparatorM.setDescription("Meta");
        comparatorM.setComparation(comparationGoal);
        comparatorM.setPointer(pointer);
        delegate.createComparator(comparatorM);

        //Populate DimensionalCut
        DimensionalCut dimensionalcut1 = (DimensionalCut) Factory.getBean("DimensionalCut");
        dimensionalcut1.setPointer(pointer);
        delegate.createDimensionalCut(dimensionalcut1);

        DimensionalCut dimensionalcut2 = (DimensionalCut) Factory.getBean("DimensionalCut");
        dimensionalcut2.setPointer(pointer);
        delegate.createDimensionalCut(dimensionalcut2);

        DimensionalCut dimensionalcut3 = (DimensionalCut) Factory.getBean("DimensionalCut");
        dimensionalcut3.setPointer(pointer);
        delegate.createDimensionalCut(dimensionalcut3);

        //Populate DimensionalCut-DimensionalMember
        DimensionalCutMember dcm = (DimensionalCutMember) Factory.getBean("DimensionalCutMember");
        dcm.setDimensionMember(dimensionmember1);
        dcm.setDimensionalCut(dimensionalcut1);
        delegate.createDimensionalCutMember(dcm);

        DimensionalCutMember dcm2 = (DimensionalCutMember) Factory.getBean("DimensionalCutMember");
        dcm2.setDimensionMember(dimensionmember2);
        dcm2.setDimensionalCut(dimensionalcut2);
        delegate.createDimensionalCutMember(dcm2);

        DimensionalCutMember dcm3 = (DimensionalCutMember) Factory.getBean("DimensionalCutMember");
        dcm3.setDimensionMember(dimensionmember3);
        dcm3.setDimensionalCut(dimensionalcut3);
        delegate.createDimensionalCutMember(dcm3);

        //Time to EA
        DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();
        TimeZone timeZone = calendar.getTimeZone();
        dfm.setTimeZone(timeZone);

        Date jan = dfm.parse("2011-01-01");
        Date feb = dfm.parse("2011-02-01");
        Date march = dfm.parse("2011-03-01");
        Date april = dfm.parse("2011-04-01");
        Date may = dfm.parse("2011-05-01");
        Date jun = dfm.parse("2011-06-01");

        //Populate ComparatorDimensionalCut - TOTAL REGION

        ComparatorDimensionalCut cdc = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc.setComparator(comparatorR);
        cdc.setDimensionalCut(dimensionalcut1);
        cdc.setRegisterDate(march);
        cdc.setRegister((Double) 220.00);
        delegate.createComparatorDimensionalCut(cdc);

        ComparatorDimensionalCut cdc2 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc2.setComparator(comparatorM);
        cdc2.setDimensionalCut(dimensionalcut1);
        cdc2.setRegisterDate(march);
        cdc2.setRegister((Double) 210.00);
        delegate.createComparatorDimensionalCut(cdc2);


        //Populate ComparatorDimensionalCut - MX
        ComparatorDimensionalCut cdc5 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc5.setComparator(comparatorR);
        cdc5.setDimensionalCut(dimensionalcut2);
        cdc5.setRegisterDate(march);
        cdc5.setRegister((Double) 110.00);
        delegate.createComparatorDimensionalCut(cdc5);

        ComparatorDimensionalCut cdc6 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc6.setComparator(comparatorM);
        cdc6.setDimensionalCut(dimensionalcut2);
        cdc6.setRegisterDate(march);
        cdc6.setRegister((Double) 120.00);
        delegate.createComparatorDimensionalCut(cdc6);

        //Populate ComparatorDimensionalCut - CR
        ComparatorDimensionalCut cdc3 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc3.setComparator(comparatorR);
        cdc3.setDimensionalCut(dimensionalcut3);
        cdc3.setRegisterDate(march);
        cdc3.setRegister((Double) 110.00);
        delegate.createComparatorDimensionalCut(cdc3);

        ComparatorDimensionalCut cdc4 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc4.setComparator(comparatorM);
        cdc4.setDimensionalCut(dimensionalcut3);
        cdc4.setRegisterDate(march);
        cdc4.setRegister((Double) 90.00);
        delegate.createComparatorDimensionalCut(cdc4);

        //April Period
        ComparatorDimensionalCut cdc7 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc7.setComparator(comparatorR);
        cdc7.setDimensionalCut(dimensionalcut1);
        cdc7.setRegisterDate(april);
        cdc7.setRegister((Double) 500.00);
        delegate.createComparatorDimensionalCut(cdc7);

        ComparatorDimensionalCut cdc8 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc8.setComparator(comparatorM);
        cdc8.setDimensionalCut(dimensionalcut1);
        cdc8.setRegisterDate(april);
        cdc8.setRegister((Double) 450.00);
        delegate.createComparatorDimensionalCut(cdc8);

        //Populate ComparatorDimensionalCut - MX
        ComparatorDimensionalCut cdc11 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc11.setComparator(comparatorR);
        cdc11.setDimensionalCut(dimensionalcut2);
        cdc11.setRegisterDate(april);
        cdc11.setRegister((Double) 280.00);
        delegate.createComparatorDimensionalCut(cdc11);

        ComparatorDimensionalCut cdc12 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc12.setComparator(comparatorM);
        cdc12.setDimensionalCut(dimensionalcut2);
        cdc12.setRegisterDate(april);
        cdc12.setRegister((Double) 250.00);
        delegate.createComparatorDimensionalCut(cdc12);

        //Populate ComparatorDimensionalCut - CR
        ComparatorDimensionalCut cdc9 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc9.setComparator(comparatorR);
        cdc9.setDimensionalCut(dimensionalcut3);
        cdc9.setRegisterDate(april);
        cdc9.setRegister((Double) 220.00);
        delegate.createComparatorDimensionalCut(cdc9);

        ComparatorDimensionalCut cdc10 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
        cdc10.setComparator(comparatorM);
        cdc10.setDimensionalCut(dimensionalcut3);
        cdc10.setRegisterDate(april);
        cdc10.setRegister((Double) 200.00);
        delegate.createComparatorDimensionalCut(cdc10);



        //Create ScorecardRelation
        ScoreCardRelation scoreCardRelation1= (ScoreCardRelation) Factory.getBean("ScoreCardRelation");
        scoreCardRelation1.setScoreCard(mapScorecard);
        scoreCardRelation1.setOtherScoreCard(perspectiveScorecard);
        scoreCardRelation1.setPercentFactor(1D);
        delegate.createScoreCardRelation(scoreCardRelation1);

        ScoreCardRelation scoreCardRelation2= (ScoreCardRelation) Factory.getBean("ScoreCardRelation");
        scoreCardRelation2.setScoreCard(perspectiveScorecard);
        scoreCardRelation2.setOtherScoreCard(objectiveScorecard);
        scoreCardRelation1.setPercentFactor(1D);
        delegate.createScoreCardRelation(scoreCardRelation2);

        ScoreCardRelation scoreCardRelation3= (ScoreCardRelation) Factory.getBean("ScoreCardRelation");
        scoreCardRelation3.setScoreCard(objectiveScorecard);
        scoreCardRelation3.setOtherScoreCard(pointer);
        scoreCardRelation1.setPercentFactor(1D);
        delegate.createScoreCardRelation(scoreCardRelation3);

        //request.setAttribute("mapScorecard", mapScorecard);

        return new ModelAndView("exito");
    }
}
